package com.mr.base.action;

import java.io.IOException;
import java.util.List;

import com.mr.base.domain.Item;
import com.mr.base.domain.ItemType;
import com.mr.base.service.ItemService;
import com.skysz.framework.action.AbstractBaseAction;
import com.skysz.framework.action.ActionResponse;
import com.skysz.framework.action.ResponseType;
import com.skysz.framework.page.PageHandler;
import com.skysz.framework.plugin.dhtmlx.grid.Column;
import com.skysz.framework.plugin.dhtmlx.grid.formatter.StringColumnFomatter;
import com.skysz.framework.plugin.dhtmlx.grid.json.ArrayJsonGrid;
import com.skysz.framework.plugin.dhtmlx.tree.xml.XMLTree;
import com.skysz.framework.utils.Result;
/**
 * 项目管理
 * @author zhaow
 *
 */
public class ItemAction  extends AbstractBaseAction {
	private static final long serialVersionUID = 5458875121500385304L;
	
	private ItemService itemService;
	
	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	/**
	 * 查询项目类型树
	 * @return
	 * @throws Exception
	 */
	public String findItemTypeTree() throws Exception {
		List<ItemType> orgs = this.itemService.findItemTypeTree();
		
		
		XMLTree xmlTree = new XMLTree(0L, orgs);
		xmlTree.setIsRootOpen(true);
		String xml = xmlTree.toXML();
		this.write(xml);
		return null;
	}
	
	public ItemType findItemTypeById() throws Exception {
		Long itemTypeId = super.getParameter("itemTypeId", Long.class);
		return this.itemService.findItemTypeById(itemTypeId);
	}
	
	public Item findItemById() throws Exception {
		Long itemTypeId = super.getParameter("itemTypeId", Long.class);
		return this.itemService.loadItemById(itemTypeId);
	}
	
	/**
	 * 查询项目信息
	 * @return
	 * @throws IOException
	 */
	public String findItem() throws Exception {
		StringBuffer hql = new StringBuffer();
		Long companyId = this.getCurrentOrg().getId();
		hql.append("select t.id,t.name,t.price,t.qcode,t.itemType.name,t.itemType.rootId||'', ");
		hql.append("t.enable from Item t left join t.itemType where t.companyId=").append(companyId);
		return this.findItems(hql);
	}
	
	/**
	 * 查询项目信息
	 * @return
	 * @throws IOException
	 */
	public String findSelectItem() throws Exception {
		String itemIds = super.getParameter("itemIds");
		StringBuffer hql = new StringBuffer();
		hql.append("select t.id,0,t.name,t.price,t.qcode,t.itemType.name,t.itemType.rootId||'', ");
		hql.append("t.price from Item t left join t.itemType where 1=1 ");
		if (itemIds != null && !"".equals(itemIds.trim()))
			hql.append(" and t.id not in(").append(itemIds).append(") ");
		return this.findItems(hql);
	}
	
	
	private String findItems(StringBuffer hql) throws IOException {
		ArrayJsonGrid grid = new ArrayJsonGrid();
		grid.setIsNeedIndex(true);
		PageHandler ph = new PageHandler(grid);
		ph.setHql(hql.toString());
		ph.addPartHql(" and t.name like '%'||?||'%' ","name");
		ph.addPartHql(" and t.qcode like '%'||?||'%' ","qcode");
		ph.addPartHql(" and t.itemType.id=? ","parentId", Long.class);
		grid.addColumn(5, new Column("t.itemType.rootId",new StringColumnFomatter("1,2","服务,产品")));
		ph.load();
		ph.output();
		return null;
	}
	
	/**
	 * 新增项目类型
	 * @return
	 * @throws Exception
	 */
	public ActionResponse insertItemType() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE);
		ItemType c = super.creatBean(ItemType.class, true);
		Long parentId = super.getParameter("parentId", Long.class);
		ItemType p = new ItemType();
		c.setOrg(this.getCurrentOrg()) ;
		p.setId(parentId);
		c.setParent(p);
		super.setWho(c);
		
		this.itemService.insertItemType(c);
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	} 
	
	/**
	 * 修改项目类型
	 * @return
	 * @throws Exception
	 */
	public ActionResponse updateItemType() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE);
		ItemType c = super.creatBean(ItemType.class, true);
		
		
		this.itemService.updateItemType(c);
		
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	}
	
	/**
	 * 禁用项目类型
	 * @return
	 * @throws Exception
	 */
	public ActionResponse disableItemType() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE);
		Long itemTypeId = super.getParameter("itemTypeId", Long.class);
		
		this.itemService.disableItemType(itemTypeId);
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	}
	
	
	/**
	 * 新增项目
	 * @return
	 * @throws Exception
	 */
	public ActionResponse insertItem() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE);
		
		Long itemTypeId = super.getParameter("itemTypeId", Long.class);
		
		Item c = super.creatBean(Item.class, true);
		c.setOrg(this.getCurrentOrg()) ;
		c.setEnable(true);
		ItemType it = new ItemType();
		it.setId(itemTypeId);
		c.setItemType(it);
		super.setWho(c);
		
		Result r = this.itemService.insertItem(c);
		//保存失败
		if (Result.CODE_FAILURE.equals(r.getCode())) {
			ar.setResponseType(ResponseType.INFO);
			ar.setInfo(r.getInfo());
			return ar;
		}
		
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	}
	
	/**
	 * 修改项目
	 * @return
	 * @throws Exception
	 */
	public ActionResponse updateItem() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE);
		Long itemTypeId = super.getParameter("itemType.id", Long.class);
		Item c = super.creatBean(Item.class, true);
		ItemType it = new ItemType();
		it.setId(itemTypeId);
		c.setItemType(it);
		super.setWho(c);
		
		Result r = this.itemService.updateItem(c);
		//保存失败
		if (Result.CODE_FAILURE.equals(r.getCode())) {
			ar.setResponseType(ResponseType.INFO);
			ar.setInfo(r.getInfo());
			return ar;
		}
		
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	} 
	
	/**
	 * 禁用项目
	 * @return
	 * @throws Exception
	 */
	public ActionResponse disableItem() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE);
		Long itemId = super.getParameter("itemId", Long.class);
		
		this.itemService.disableItem(itemId);
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	}
}
